<%@ page language="java" import="java.util.*" import="com.jspsmart.upload.*" pageEncoding="UTF-8"%>
<%@ page language="java" import="java.io.*" %>
<%@ page language="java" import="java.awt.*" %>
<%@ page language="java" import="java.awt.image.*" %>
<%@ page language="java" import="com.sun.image.codec.jpeg.*" %>
<%@ page language="java" import="software.Use_user" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>软件开发交流平台</title>
<link href="../css/register.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
	function check(){
		if(form2.username.value==""){
			alert("请输入你的用户名");
			form2.username.focus();
			return false;
		}
		else if(form2.Rname.value==""){
			alert("请输入你昵称");
			form2.Rname.focus();
			return false;
		}
		else if(form2.password.value==""){
			alert("请输入你注册的密码");
			form2.password.focus();
			return false;
		}
		else if(form2.pasa.value==""){
			alert("请输入确认密码");
			form2.pasa.focus();
			return false;
		}
		else if(form2.password.value!=form1.pasa.value){
			alert("你输入的两次密码不一致");
			return false;
		}
		else if(form2.email.value==""){
			alert("请输入你的Email址");
			form2.email.focus();
			return false;
		}
		else 
		{
			return true;
		}
	}
</script>
  </head>
  <body>
   <%
	int fileSizeAllow=2097152,sex=1,MarkP=0,MarkU=2,MarkE=2,Mark=0;
  	  /*//MarkP 标志是否有头像
  	  //Mark用户名是否被注册
  	  //MarkE 标志邮箱是否被注册
  	  */
  	String username=new String();
	String nickname=new String();
	String password=new String();
	String email=new String();
	String birthday=new String();
	String Phosrc=null;
	SmartUpload su = new SmartUpload();	
	Use_user userregister=new Use_user();
	String NowTime=null;
	
	request.setCharacterEncoding("UTF-8");
	try{
	 su.initialize(pageContext);
	 su.upload();				//获取表单失败
	 username=su.getRequest().getParameter("username");
	 nickname=su.getRequest().getParameter("Rname");
	 password=su.getRequest().getParameter("password");
	 email=su.getRequest().getParameter("email");
	 sex=Integer.parseInt(su.getRequest().getParameter("sex"));
	 birthday=su.getRequest().getParameter("birthday");
	 
	}catch(Exception e){
	
	}
	if(username!=null)
	if(!username.equals("")){
		/*
		//有注册行为;
		*/
		MarkU=0;
		MarkE=0;
		try{
			///上传照片
			 com.jspsmart.upload.File file = su.getFiles().getFile(0);	//取得指定位移处的文件对象File
			 if (!file.isMissing()){					
			//文件存在
				String fileName=file.getFileName();
			 	MarkP=1;
				int fileSize = file.getSize();			//取得文件的大小
				String extName = file.getFileExt().toLowerCase();
				if (fileSize > fileSizeAllow) 
				{
					out.print("<script>alert('文件太大！')</script>");
				}
				if (!(extName.equals("gif")||extName.equals("jpg") )) {
					out.print("<script>alert('文件格式不正确！')</script>");
				}
				NowTime=new java.util.Date().toLocaleString().replaceAll(":","-").replaceAll(" ","-");
				String fileNameNew=NowTime+"."+ extName;// 以日期命名的格式
				Phosrc=NowTime+".jpg";
				file.saveAs("TempFile/"+fileNameNew, SmartUpload.SAVE_VIRTUAL);		
		 	}
		}catch(Exception e){
		 	System.out.println("上传图片产生错误！");
		 	}
		 userregister.setUsername(username);
		 userregister.setEmail(email);
		 userregister.setNickName(nickname);
		 userregister.setPassword(password);
		 userregister.setBirthday(birthday);
		 userregister.setPhoto(Phosrc);
		if(userregister.Che_NameR())
			MarkU=1;
		if(userregister.Che_EmailR())
			MarkE=1;
		if(MarkU==1&&MarkE==1){
			if(userregister.RegisterR())
				{
					Mark=1;//注册成功
					String saveurl=request.getRealPath("/")+"TempFile"+"\\"+Phosrc;
					java.io.File Jfile = new java.io.File(saveurl); 	//读入刚才上传的文件
					Image src= javax.imageio.ImageIO.read(Jfile); 			//构造Image对象
					float tagsize=150;									//最大的宽度和高度
					int old_w=src.getWidth(null); 						//得到源图宽
					int old_h=src.getHeight(null); 						//得到源图长 
				                     
					int tempsize;
					float tempdouble; 
					if(old_w>old_h){
					tempdouble=old_w/tagsize;
					}else{
					tempdouble=old_h/tagsize;
					}
					int new_w=Math.round(old_w/tempdouble);//计算新图长宽
					int new_h=Math.round(old_h/tempdouble);
					BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
					tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); 			//绘制缩小后的图
					
					FileOutputStream newimage=new FileOutputStream(request.getRealPath("/")+"Photo\\"+Phosrc);           //输出到文件流
					JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);         
					encoder.encode(tag); 				//近JPEG编码,结果是所有的图都转换成了jpg
					newimage.close();
					response.sendRedirect("register_success.jsp?id="+userregister.getId());
					out.println("<script>alert('恭喜你，注册成功！')</script>");
				}
			}
			else{
			 		out.println("<script>alert('注册失败')</script>");}
				}
	%>
<jsp:include page="head.jsp" />
<div id="contianer">
  <div id="register">
  <form action="register.jsp" method="post"  enctype="multipart/form-data" name="form2" onsubmit="return check()">
    <input name="username" type="text" class="text" style="margin-top:120px;" value="<%if(MarkU==0)out.print(username+"  已被注册");%>"/>
    <input name="Rname" type="text" class="text" style="margin-top:48px;" value="<%=nickname%>"/>
    <input name="password" type="password" class="text" style="margin-top:48px;" />   
    <input name="pasa" type="password" class="text" style="margin-top:48px;"/>  
        <table id="form0" width="180" style="font-size:16px; font-weight:bolder;">
          <tr>
              <td><label>
                <input type="radio" name="sex" value="1" <%if(sex==1)out.print("checked"); %>/>
                男</label></td>
              <td><label>
                <input type="radio" name="sex" value="0" <%if(sex==0)out.print("checked"); %> />
                女</label></td>
          </tr>
        </table>
    <input name="email" type="text" class="text" style=" margin-top:40px;"  value="<%if(MarkE==0)out.print(email+"  已被注册");%>"/>
        <input name="birthday" type="text" class="text" style="margin-top:50px;"  value="<%=birthday%>"/>
        <input name="filename" type="file" style="margin-top:55px; margin-left:180px;"/>
        <input name="提交" type="submit"  value=""style="background-image:url(../images/register/register-tj.jpg); width:88px; height:31px; margin-top:50px; margin-left:105px; border:0px; padding:0px; " />
       <input name="" type="reset"  value="" style="background-image:url(../images/register/register-cz.jpg); width:88px; height:31px; margin-top:50px; margin-left:89px; padding:0px; border:0px;"  />
  </form>
  </div>
</div>
 	<jsp:include page="footer.jsp"/>
  </body>
</html>
